#include "stm32f10x.h"  
#include "Serial.h"
#include "OLED.h"
#include "Delay.h"
#include <string.h>

int ESP8266_strctn(char *str,char * needle) {
	char *p;
	char *np;
	int found = 0;
	while(*str != 0) {
		if(*str == *needle) {
			found = 1;
			p = str;
			np = needle;
			while(*np != 0) {
				if(*p != *np) {
					found = 0;
					break;
				}
				p++;
				np++;
			}
			if(found)
				return found;
		}
		str++;
	}
	return found;
}

void ESP8266_Init(void) {
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_SET);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出，复用指的是输出由片内外设(TX1)控制，而不是由CPU控制
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;//可选奇偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位长度
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8bit
	USART_Init(USART1,&USART_InitStructure);
	
	USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //配置接收中断
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
}

int ESP8266_WaitFor(char* forWhar,int timeout) {
	int i;
	u8* sstr;
	while(1) {
		if(timeout > 0 )timeout--;
		if(timeout == 0) {
			return 0;
		}
		if(!Serial_GetRxFlag())
			continue;
		
		sstr = Serial_GetRxString();
		for(i=0;i<Serial_GetDataPointer();i++) {
			if(sstr[i] == 0) {
				sstr[i] = '_';
			}
		}
		//OLED_ShowString(3,1,(char*)sstr);
		//printf("\n(%d)%d[%s\n]\n",Serial_GetDataPointer(),strlen((char*)sstr),(char*)sstr);
		if(ESP8266_strctn((char*)sstr,forWhar)) {
			//printf(">>>>>>>\n");
			//OLED_ShowChar(4,12,']');
			Serial_ClearRxString();
			return 1;
		}
		if(Serial_GetOvFlag()) {
			Serial_ClearRxString();
		}
	}
}

int ESP8266_SendCIPMessage(char *msg) {
	char buffer[32];
	sprintf(buffer,"AT+CIPSEND=%d\r\n",strlen(msg));
	Serial_SendString(buffer);
	ESP8266_WaitFor(">",1000000);
	//printf("sending %d...\r\n",strlen((char*)response));
	Serial_SendString(msg);
	return ESP8266_WaitFor("OK",1000000);
}

int ESP8266_SendCmdAndWaitOK(char *cmd) {
	Serial_SendString(cmd);
	return ESP8266_WaitFor("OK",1000000);
}

int pass = 00;

void ESP8266_StartServer(void) {
	OLED_ShowString(1,1,"Starting Model");
	OLED_ShowString(2,1,"Prog:[");
	OLED_ShowChar(2,12,']');
	ESP8266_SendCmdAndWaitOK("AT\r\n") && (pass |= 0x10 >> 0);
	OLED_ShowBinNum(2,7,pass,5);
	ESP8266_SendCmdAndWaitOK("AT+CWMODE=1\r\n") && (pass |= 0x10 >> 1);
	OLED_ShowBinNum(2,7,pass,5);;
	Serial_SendString("AT+RST\r\n");
	ESP8266_WaitFor("OK",-1)  && (pass |= 0x10 >> 2);
	OLED_ShowBinNum(2,7,pass,5);
	Delay_s(2);
	Serial_SendString("AT+CWJAP=\"410\",\"12335678@410\"\r\n");
	ESP8266_WaitFor("GOT IP",-1);
	ESP8266_WaitFor("OK",-1)  && (pass |= 0x10 >> 3);
	OLED_ShowBinNum(2,7,pass,5);
	ESP8266_SendCmdAndWaitOK("AT+CIPSTART=\"UDP\",\"192.168.31.94\",15533\r\n") && (pass |= 0x10 >> 4);
	OLED_ShowBinNum(2,7,pass,5);
	if(pass == 0x1f) {
		OLED_ShowString(1,1,"Model Running ");
	}
}
